package scripting.example2;

/**
 * @Author: webmaster@pavel-kaminsky.com
 * @Date: 31/08/11
 */
import javax.script.*;
import java.io.File;

public class InvokeScriptMethod {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");

        // JavaScript code in a String. This code defines a script object 'obj'
        // with one method called 'hello'.
        engine.eval(new java.io.FileReader(new File("resources/scripting/example2/script.js").getAbsoluteFile()));

        // javax.script.Invocable is an optional interface.
        // Check whether your script engine implements or not!
        // Note that the JavaScript engine implements Invocable interface.
        Invocable inv = (Invocable) engine;

        // get script object on which we want to call the method
        Object obj = engine.get("obj");

        // invoke the method named "hello" on the script object "obj"
        inv.invokeMethod(obj, "rock", "Pavel-Kaminsky.com " );
    }
}
